package cn.zzp.system.service.impl;

import cn.zzp.basic.service.impl.BaseServiceImpl;
import cn.zzp.system.domain.Menu;
import cn.zzp.system.mapper.MenuMapper;
import cn.zzp.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EmployeeServiceImpl
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/14 14:16
 */
@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> menuTree() {
        List<Menu> menus = menuMapper.queryAll();
        Map<Long, Menu> map = new HashMap<>();

        for (Menu menu : menus) {
            map.put(menu.getId(), menu);
        }

        List<Menu> result = new ArrayList<>();

        for (Menu menu : menus) {
            if (menu.getParent_id() == null){
                result.add(menu);
            }else {
                Menu parent = map.get(menu.getParent_id());
                parent.getChildren().add(menu);
            }

        }

        return result;
    }
}
